package net.sea.setup.action.ext.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import net.sea.commons.context.AppContext;
import net.sea.commons.util.ELUtils;
import net.sea.commons.util.InstallMsgUtils;
import net.sea.commons.util.ELUtils.EL;
import net.sea.commons.util.ShellUtils;
import net.sea.setup.action.AbstractAction;
import net.sea.setup.action.constants.Constants;

/**
 * 
 * 配置文件修改操作
 *
 * @author huachengwu 
 */
public class ExecutableFileAction extends AbstractAction {

	/*
	 * 可执行文件执行方法
	 * 
	 * @see
	 * com.ec.setup.action.IAction#install(net.sea.commons.context.AppContext,
	 * java.util.Map)
	 */
	@Override
	public void install(AppContext context, Map<String, String> parMap) {
		logger.info(parMap.get(Constants.INSTALL_DESC_KEY));
		long startTime = System.currentTimeMillis();
		String exeFile = parMap.get(Constants.EXECUTABLE_FILE_KEY);

		// 替换EL变量
		List<EL> els = ELUtils.getVarName(exeFile);
		for (EL el : els) {
			String value = (String) context.getAttribute(el.getVarName());
			exeFile = exeFile.replace(el.getElExp(), value.replace("\\", "/"));
		}

		if (logger.isDebugEnabled()) {
			logger.debug("实际可执行文件为：" + exeFile);
		}

		InstallMsgUtils.setInstallMsg("执行文件【" + new File(exeFile).getName()
				+ "】");
		// 执行安装程序
		ShellUtils.callCmd(exeFile);

		long endTime = System.currentTimeMillis();
		if (logger.isDebugEnabled()) {
			logger.debug("操作执行时间：" + (endTime - startTime) / 1000.0 + "秒");
		}
	}

	@Override
	public void uninstall(AppContext context, Map<String, String> parMap) {

	}
}
